package com.example.dao;

import com.example.pojo.Coach;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class CoachDao {
    //查找所有教练信息
    public List<Coach> coachList()
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="SELECT * FROM coach";
        return template.query(sql, new RowMapper<Coach>() {
            @Override
            public Coach mapRow(ResultSet resultSet, int i) throws SQLException {
                Coach coach=new Coach();
                coach.setId(resultSet.getInt(1));
                coach.setName(resultSet.getString(2));
                coach.setAge(resultSet.getInt(3));
                coach.setSex(resultSet.getString(4));
                coach.setPhone(resultSet.getString(5));
                coach.setPass(resultSet.getString(6));
                return coach;
            }
        });

    }
    //添加教练
    public Boolean addCoach(Coach coach)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="INSERT INTO coach(name,age,sex,phone,pass)" +
                " VALUES(?,?,?,?,?)";
        return template.update(sql,
                coach.getName(),
                coach.getAge(),
                coach.getSex(),
                coach.getPhone(),
                coach.getPass())>0;
    }

    //修改教练的账户密码
    public Boolean setCoachPass(String phone,String pass)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="UPDATE coach SET pass=? WHERE phone=?";
        return template.update(sql,pass,phone)>0;
    }
    //删除教练
    public Boolean deleteCoach(String phone)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="DELETE FROM coach WHERE phone=?";
        return template.update(sql,phone)>0;
    }


    @Test
    public void mytest()
    {
      CoachDao dao=new CoachDao();
      /*System.out.println(dao.coachList());
      Coach coach=new Coach();
      coach.setName("冯世伦");
      coach.setAge(50);
      coach.setSex("男");
      coach.setPhone("15855555555");
      coach.setPass("123456");
      System.out.println(dao.addCoach(coach));*/
      //System.out.println(dao.coachList());
      // System.out.println(dao.setCoachPass("15855555555","test2"));
      //System.out.println(dao.coachList());
    }
}
